//
//  XMPhoneCodeView.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/8.
//

#import "XMPhoneCodeView.h"

@interface XMPhoneCodeView ()

@property (nonatomic, strong) UILabel *phoneLabel;
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) UITextField *inputTextField;

@property (nonatomic, strong) NSMutableArray *textFieldArr;
//@property (nonatomic, strong)

@end

@implementation XMPhoneCodeView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(0, kNavagationBarH - 44, 44, 44) image:@"back_icon_black" selectImg:@"back_icon_black"];
    [backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:backBtn];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), kNavagationBarH + KScaleWidth(5.0), self.width - KScaleWidth(28.0)*2, KScaleWidth(46.0)) font:KBoldFontSize(32.0) textColor:kColorHEX(0x221232)];
    titleLabel.text = @"请输入验证码";
    [self addSubview:titleLabel];
    [self addSubview:self.phoneLabel];
    _phoneLabel.y = titleLabel.maxY + KScaleWidth(5.0);
    [self addSubview:self.inputTextField];
    for (int i = 0; i < 4; i ++) {
        CGFloat frameX = (self.width - KScaleWidth(44.0)*4)/7;
        UIButton *codeBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(20.0) frame:CGRectMake(frameX*2 + (KScaleWidth(44.0) + frameX)*i, _phoneLabel.maxY + KScaleWidth(118.0), KScaleWidth(44.0), KScaleWidth(44.0)) title:@"" titleColor:kColorHEX(0x222222)];
        codeBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        codeBtn.backgroundColor = kColorHEX(0xF5F3F7);
        [codeBtn setCornerRadius:codeBtn.height/2];
        [codeBtn addTarget:self action:@selector(inputAction:) forControlEvents:UIControlEventTouchUpInside];
        codeBtn.tag = 1000 + i;
        [self addSubview:codeBtn];
        [self.textFieldArr addObject:codeBtn];
    }
    _inputTextField.y = _phoneLabel.maxY + KScaleWidth(118.0);
    [_inputTextField becomeFirstResponder];
    
    UIButton *getAgainBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:CGRectMake((self.width - KScaleWidth(80.0))/2, _inputTextField.maxY + KScaleWidth(20.0), KScaleWidth(80.0), KScaleWidth(28.0)) title:@"重新获取" titleColor:kColorHEX(0xDD4B39)];
    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:@"重新获取" attributes:@{NSFontAttributeName : KFontSize(14.0), NSForegroundColorAttributeName : kColorHEX(0xDD4B39), NSFontAttributeName : KFontSize(24.0), NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle), NSUnderlineColorAttributeName : kColorHEX(0xDD4B39)}];
    [getAgainBtn setAttributedTitle:attrStr forState:UIControlStateNormal];
    [getAgainBtn addTarget:self action:@selector(regetCodeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:getAgainBtn];
    [self addSubview:self.loginBtn];
    _loginBtn.y = getAgainBtn.maxY + KScaleWidth(65.0);
}

- (void)setContentWithPhoneNum:(NSString *)phoneNum {
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"已发送验证码至  %@", phoneNum] attributes:@{NSForegroundColorAttributeName : kColorHEX(0x3B5998), NSFontAttributeName : KFontSize(16.0)}];
    [attrStr addAttributes:@{NSForegroundColorAttributeName : kColorHEX(0x988DA2)} range:NSMakeRange(0, 7)];
    [_phoneLabel setAttributedText:attrStr];
}

- (void)inputAction:(UIButton *)button {
    [_inputTextField becomeFirstResponder];
}

- (void)regetCodeAction:(UIButton *)button {
    self.reGetCodeBlock ? self.reGetCodeBlock() : nil;
}

- (void)inputChanged:(UITextField *)textField {
    textField.text = [textField.text substringWithRange:NSMakeRange(0, MIN(textField.text.length, 4))];
    for (int i = 0; i < MIN(4, textField.text.length); i ++) {
        UIButton *btn = [self viewWithTag:1000 + i];
        NSString *title = textField.text.length > i ? [textField.text substringWithRange:NSMakeRange(i, 1)] : @"";
        [btn setTitle:title forState:UIControlStateNormal];
    }
    _loginBtn.enabled = textField.text.length >= 4;
}

//- (void)inputChanged:(UITextField *)textField {
//    if (textField.text.length > 0) {
//        if (textField.tag == 1003) {
//            _loginBtn.enabled = YES;
//        }
//        else {
//            UITextField *nextTextField = [self viewWithTag:textField.tag + 1];
//            [nextTextField becomeFirstResponder];
//        }
//    }
//    else {
//        _loginBtn.enabled = NO;
//        UITextField *nexeTextField = [self viewWithTag:MAX(1000, textField.tag - 1)];
//        [nexeTextField becomeFirstResponder];
//    }
//}

- (void)loginAction:(UIButton *)button {
    if (_inputTextField.text.length < 4) {
        [MBProgressHUD showError:@"请输入完整的验证码"];
        return;
    }
    self.loginBlock ? self.loginBlock(_inputTextField.text) : nil;
}

#pragma mark - Init
- (UILabel *)phoneLabel {
    if (!_phoneLabel) {
        _phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), 0, self.width - KScaleWidth(28.0)*2, KScaleWidth(33.0))];
    }
    return _phoneLabel;
}

- (UITextField *)inputTextField {
    if (!_inputTextField) {
        CGFloat frameX = (self.width - KScaleWidth(44.0)*4 - KScaleWidth(16.0)*3)/2;
        _inputTextField = [[UITextField alloc] initWithFrame:CGRectMake(frameX, 0, KScaleWidth(44.0), KScaleWidth(44.0))];
        _inputTextField.textColor = kColorHEX(0xffffff);
        _inputTextField.alpha = 0;
        [_inputTextField addTarget:self action:@selector(inputChanged:) forControlEvents:UIControlEventEditingChanged];
        _inputTextField.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _inputTextField;
}

- (UIButton *)loginBtn {
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(16.0) frame:CGRectMake(KScaleWidth(28.0), 0, self.width - KScaleWidth(28.0)*2, KScaleWidth(44.0)) title:@"登录" titleColor:kColorHEX(0xCBC3D2)];
        _loginBtn.backgroundColor = kColorHEX(0xF5F3F7);
        [_loginBtn setCornerRadius:_loginBtn.height/2];
        [_loginBtn setBackgroundImage:[UIImage imageWithColor:kColorHEX(0xF5F3F7)] forState:UIControlStateDisabled];
         [_loginBtn setTitleColor:kColorHEX(0xCBC3D2) forState:UIControlStateDisabled];
         [_loginBtn setBackgroundImage:[UIImage imageWithColor:kColorHEX(0xFFD800)] forState:UIControlStateNormal];
         [_loginBtn setTitleColor:kColorHEX(0x333333) forState:UIControlStateNormal];
        [_loginBtn addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
        _loginBtn.enabled = NO;
    }
    return _loginBtn;
}

- (NSMutableArray *)textFieldArr {
    if (!_textFieldArr) {
        _textFieldArr = [NSMutableArray array];
    }
    return _textFieldArr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
